package com.linlang.grasharepro.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.linlang.grasharepro.R;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.utils.FileUtiles;

import java.util.List;

/**
 * 商家WIFI适配器
 * Created by pengtao on 2016/6/3.
 */
public class BiYeShareAdapter extends BaseAdapter {

    /**
     * 分享信息列表
     */
    private List<ShareCoenten> list;

    public List<ShareCoenten> getList() {
        return list;
    }

    public void setList(List<ShareCoenten> list) {
        this.list = list;
    }

    public Context getCtx() {
        return ctx;
    }

    public void setCtx(Context ctx) {
        this.ctx = ctx;
    }


    private Context ctx;


    public BiYeShareAdapter(List<ShareCoenten> list, Context ctx) {
        this.list = list;
        this.ctx = ctx;
        // 实例化获取图片的类

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder hold;
        if (convertView == null) {
            hold = new Holder();
            convertView = View.inflate(ctx, R.layout.item_biye_content, null);
            hold.mTitle = (TextView) convertView.findViewById(R.id.share_title);//标题
            hold.mContent = (TextView) convertView.findViewById(R.id.share_content);//内容
            hold.mUser = (TextView) convertView.findViewById(R.id.share_username);//用户名
            hold.mTime = (TextView) convertView.findViewById(R.id.share_time);//发布时间
            hold.mType = (ImageView) convertView.findViewById(R.id.share_type);//用户类型
            hold.mCollection = (TextView) convertView.findViewById(R.id.share_colllection);//收藏数
            hold.mMessage = (TextView) convertView.findViewById(R.id.share_mess);//留言数
            hold.mShare = (TextView) convertView.findViewById(R.id.share_share);//分享数
            hold.mHead = (ImageView) convertView.findViewById(R.id.share_head);
            hold.image1 = (ImageView) convertView.findViewById(R.id.biye_image1);
            hold.image2 = (ImageView) convertView.findViewById(R.id.biye_image2);
            hold.image3 = (ImageView) convertView.findViewById(R.id.biye_image3);
            hold.image4 = (ImageView) convertView.findViewById(R.id.biye_image4);
            hold.image5 = (ImageView) convertView.findViewById(R.id.biye_image5);
            hold.image6 = (ImageView) convertView.findViewById(R.id.biye_image6);
            hold.image7 = (ImageView) convertView.findViewById(R.id.biye_image7);
            hold.image8 = (ImageView) convertView.findViewById(R.id.biye_image8);
            hold.image9 = (ImageView) convertView.findViewById(R.id.biye_image9);
            convertView.setTag(hold);
        } else {
            hold = (Holder) convertView.getTag();
        }

        if (list != null && list.size() > 0) {
            ShareCoenten scd = list.get(position);
            hold.mTitle.setText(scd.getTitle());
            hold.mHead.setTag(scd.getHeadUrl());
            hold.mUser.setText(scd.getUserName());
            hold.mTime.setText(scd.getTime());
            hold.mContent.setText(scd.getContent());
            hold.mCollection.setText(scd.getCollectioncount());
            hold.mMessage.setText(scd.getMessagecount());
            hold.mShare.setText(scd.getSharecount());

            if (scd.getType().equals("在校生"))
                hold.mType.setImageResource(R.drawable.inschool);
            else
                hold.mType.setImageResource(R.drawable.outschool);


            if (scd.getImgUrl() != null && scd.getImgUrl().length() != 0 && scd.getImgUrl() != " ") {
                hold.image1.setVisibility(View.VISIBLE);
                Bitmap bitmap0 = FileUtiles.getSmallBitmap(scd.getImgUrl());
                hold.image1.setImageBitmap(bitmap0);
            }
            if (scd.getImgUrl1() != null && scd.getImgUrl1().length() != 0 && scd.getImgUrl1() != " ") {
                hold.image2.setVisibility(View.VISIBLE);
                Bitmap bitmap1 = FileUtiles.getSmallBitmap(scd.getImgUrl1());
                hold.image2.setImageBitmap(bitmap1);
            }
            if (scd.getImgUrl2() != null && scd.getImgUrl2().length() != 0 && scd.getImgUrl2() != " ") {
                hold.image3.setVisibility(View.VISIBLE);
                Bitmap bitmap2 = FileUtiles.getSmallBitmap(scd.getImgUrl2());
                hold.image3.setImageBitmap(bitmap2);
            }
            if (scd.getImgUrl3() != null && scd.getImgUrl3().length() != 0 && scd.getImgUrl3() != " ") {
                hold.image4.setVisibility(View.VISIBLE);
                Bitmap bitmap3 = FileUtiles.getSmallBitmap(scd.getImgUrl3());
                hold.image4.setImageBitmap(bitmap3);
            }
            if (scd.getImgUrl4() != null && scd.getImgUrl4().length() != 0 && scd.getImgUrl4() != " ") {
                hold.image5.setVisibility(View.VISIBLE);
                Bitmap bitmap4 = FileUtiles.getSmallBitmap(scd.getImgUrl4());
                hold.image5.setImageBitmap(bitmap4);
            }
            if (scd.getImgUrl5() != null && scd.getImgUrl5().length() != 0 && scd.getImgUrl5() != " ") {
                hold.image6.setVisibility(View.VISIBLE);
                Bitmap bitmap5 = FileUtiles.getSmallBitmap(scd.getImgUrl5());
                hold.image6.setImageBitmap(bitmap5);
            }
            if (scd.getImgUrl6() != null && scd.getImgUrl6().length() != 0 && scd.getImgUrl6() != " ") {
                hold.image7.setVisibility(View.VISIBLE);
                Bitmap bitmap6 = FileUtiles.getSmallBitmap(scd.getImgUrl6());
                hold.image7.setImageBitmap(bitmap6);
            }
            if (scd.getImgUrl7() != null && scd.getImgUrl7().length() != 0 && scd.getImgUrl7() != " ") {
                hold.image8.setVisibility(View.VISIBLE);
                Bitmap bitmap7 = FileUtiles.getSmallBitmap(scd.getImgUrl7());
                hold.image8.setImageBitmap(bitmap7);
            }
            if (scd.getImgUrl8() != null && scd.getImgUrl8().length() != 0 && scd.getImgUrl8() != " ") {
                hold.image9.setVisibility(View.VISIBLE);
                Bitmap bitmap8 = FileUtiles.getSmallBitmap(scd.getImgUrl8());
                hold.image9.setImageBitmap(bitmap8);
            }

            // 设置默认显示的图片
            Bitmap bitmap = FileUtiles.getSmallBitmap(scd.getHeadUrl());
            hold.mHead.setImageBitmap(bitmap);
//            hold.mHead.setImageResource(R.drawable.atm);

        }


        /**
         Bitmap bit = loadImg.loadImage(hold.mHead, ConstantsModel.SHOPLISTIMGURL
         + list.get(position).getHeadUrl(), new LoadImg.ImageDownloadCallBack() {
        @Override public void onImageDownload(ImageView imageView, Bitmap bitmap) {
        // 网络交互时回调进来防止错位
        if (hold.mHead.getTag().equals(
        ConstantsModel.SHOPLISTIMGURL + list.get(position).getHeadUrl())) {
        // 设置网络下载回来图片显示
        hold.mHead.setImageBitmap(bitmap);
        }
        }
        });

         // 从本地获取的
         if (bit != null) {
         // 设置缓存图片显示
         hold.mHead.setImageBitmap(bit);
         }*/

        return convertView;
    }

    static class Holder {
        TextView mUser, mTime, mTitle, mContent, mCollection, mMessage, mShare;
        ImageView mHead, mType, image1, image2, image3, image4, image5, image6, image7, image8, image9;
    }
}
